/***************************************************************************************************
  *Copyright(C),2010-2016,Sumscope
  *FileName	:  pinyin_search_idstore.cpp
  *Author	:  scofined.qi
  *Version	:  1.0
  *Date		:  2017/06/04
  *Desc		:  //用于主要说明此程序文件完成的主要功能
  *Relation :  
  *Others	:  //其他内容说明
  *Function :  //主要函数列表，每条记录应包含函数名及功能简要说明
  *History	:  //修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简介
***************************************************************************************************/
#include "pinyin_search_idstore.h"
#include "pinyin_search_set.h"

namespace qb{
	namespace base{
		namespace internal{
			void IndexIdStore::init(const InverseSet& iset)
			{
				static_assert(sizeof(IndexId) == sizeof(InverseSet::KeyCount), "sizeof(IndexId) must equal to sizeof(InverseSet::KeyCount)");
				int capcity = iset.size();
				if (m_first == NULL || m_capcity < capcity)
				{
					if (m_first)	delete[] m_first;
					m_capcity = m_capcity < capcity ? capcity : m_capcity;
					m_first = new IndexId[m_capcity];
				}
				if (capcity > 0)
					memcpy(m_first, &iset.at(0), capcity*sizeof(IndexId));
				m_count = capcity;
			}
			void IndexIdStore::init(const IndexId* first, int count)
			{
				resetcapcity(count);
				for (int i = 0; i < count; i++)
					m_first[m_count++] = first[i];
			}

			void IndexIdStore::resetcapcity(int capcity)
			{
				// 设置好capcity之后,不能使用>=m_cout的索引范围
				// 对于m_capcity很大,而此处的capcity很小的情况下
				// 将memset的参数调小,可以提升性能
				// 提升性能超级多,714ms ==> 4.740ms
				int num_set = capcity + 1;
				if (m_first == NULL || m_capcity < capcity)
				{
					if (m_first)	delete[] m_first;
					m_capcity = m_capcity < capcity ? capcity : m_capcity;
					m_first = new IndexId[m_capcity];
					num_set = m_capcity;
				}
				num_set = num_set >= m_capcity ? m_capcity : num_set;
				memset(m_first, 0, num_set*sizeof(IndexId));
				m_count = 0;
			}

		}
	}
}
